package com.isoft.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.isoft.entity.AirportInfo;
import com.isoft.result.R;
import com.isoft.service.AirportInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/airportInfo")
public class AirportInfoController {

    @Autowired
    private AirportInfoService airportInfoService;

    /**
     * 分页查询机场信息
     */
    @GetMapping("/page")
    public R getPage(@RequestParam(defaultValue = "1") Integer current,
                     @RequestParam(defaultValue = "10") Integer size,
                     @RequestParam(required = false) String code,
                     @RequestParam(required = false) String cnnm,
                     @RequestParam(required = false) String ennm,
                     @RequestParam(required = false) String apat) {
        Page<AirportInfo> page = new Page<>(current, size);
        QueryWrapper<AirportInfo> queryWrapper = new QueryWrapper<>();

        if (code != null && !code.isEmpty()) {
            queryWrapper.like("code", code);
        }
        if (cnnm != null && !cnnm.isEmpty()) {
            queryWrapper.like("cnnm", cnnm);
        }
        if (ennm != null && !ennm.isEmpty()) {
            queryWrapper.like("ennm", ennm);
        }
        if (apat != null && !apat.isEmpty()) {
            queryWrapper.eq("apat", apat);
        }
        queryWrapper.orderByDesc("create_time");

        IPage<AirportInfo> pageResult = airportInfoService.page(page, queryWrapper);
        return R.ok(pageResult);
    }

    /**
     * 获取所有机场信息
     */
    @GetMapping("/list")
    public R getList() {
        List<AirportInfo> list = airportInfoService.list();
        return R.ok(list);
    }

    /**
     * 根据ID获取机场信息
     */
    @GetMapping("/{id}")
    public R getById(@PathVariable Long id) {
        AirportInfo airportInfo = airportInfoService.getById(id);
        return R.ok(airportInfo);
    }

    /**
     * 新增机场信息
     */
    @PostMapping
    public R save(@RequestBody AirportInfo airportInfo) {
        boolean result = airportInfoService.save(airportInfo);
        return result ? R.ok("新增成功") : R.error("新增失败");
    }

    /**
     * 更新机场信息
     */
    @PutMapping
    public R update(@RequestBody AirportInfo airportInfo) {
        boolean result = airportInfoService.updateById(airportInfo);
        return result ? R.ok("更新成功") : R.error("更新失败");
    }

    /**
     * 删除机场信息
     */
    @DeleteMapping("/{id}")
    public R delete(@PathVariable Long id) {
        boolean result = airportInfoService.removeById(id);
        return result ? R.ok("删除成功") : R.error("删除失败");
    }

    /**
     * 批量删除机场信息
     */
    @DeleteMapping("/batch")
    public R deleteBatch(@RequestBody List<Long> ids) {
        boolean result = airportInfoService.removeByIds(ids);
        return result ? R.ok("批量删除成功") : R.error("批量删除失败");
    }
}